package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.login.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.wechat.pay.java.service.payments.model.Transaction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private PaymentInfoService paymentInfoService;

    /**
     * 微信支付
     *
     * @param paymentType
     * @param orderNo
     * @return
     */
    @TingshuLogin
    @Operation(summary = "微信支付")
    @PostMapping("/createJsapi/{paymentType}/{orderNo}")
    public Result createJsapi(@PathVariable String paymentType,
                              @PathVariable String orderNo) {
        // 获取用户id
        Long userId = AuthContextHolder.getUserId();
        // 调用service层方法
        Map<String, Object> map = wxPayService.createJsapi(paymentType, orderNo, userId);
        // 返回数据
        return Result.ok(map);
    }

    /**
     * 支付状态查询
     *
     * @param orderNo
     * @return
     */
    @Operation(summary = "支付状态查询")
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo) {
        // 调用service层方法
        Transaction transaction = wxPayService.queryPayStatus(orderNo);
        // 判断
        if (null != transaction && transaction.getTradeState().equals(Transaction.TradeStateEnum.SUCCESS)) {
            // 说明这个用户支付成功了,更新交易记录状态.
            paymentInfoService.updatePaymentStatus(transaction);
            // 返回true
            return Result.ok(true);
        }
        // 返回false
        return Result.ok(false);
    }

    /**
     * 微信支付异步通知接口
     *
     * @param request
     * @return
     */
    @Operation(summary = "微信支付异步通知接口")
    @PostMapping("/notify")
    public Map<String, Object> wxNotify(HttpServletRequest request) {
        System.out.println("异步回调通知......");
        HashMap<String, Object> result = new HashMap<>();
        Transaction transaction = wxPayService.wxNotify(request);
        if (null != transaction && transaction.getTradeState().equals(Transaction.TradeStateEnum.SUCCESS)) {
            // 更新订单状态
            paymentInfoService.updatePaymentStatus(transaction);
            // 返回成功
            result.put("code", "SUCCESS");
            result.put("message", "成功");
        } else {
            // 返回失败
            result.put("result_code", "FAIL");
            result.put("message", "失败");
        }
        return result;
    }

    /**
     * 支付测试接口
     *
     * @param paymentType
     * @param orderNo
     * @return
     */
//    @TingshuLogin
//    @Operation(summary = "微信Native下单")
//    @PostMapping("/createNative/{paymentType}/{orderNo}")
//    public Result<Map<String, Object>> createNative(@PathVariable String paymentType,
//                                                    @PathVariable String orderNo) {
//        // 获取当前用户id
//        Long userId = AuthContextHolder.getUserId();
//        // 调用service层方法
//        Map<String, Object> map = wxPayService.createNative(paymentType, orderNo, userId);
//        // 返回数据
//        return Result.ok(map);
//    }

}
